import fetch from 'utils/fetch';

/**
 * 
 * @param {*} id 
 */
export function get(id){
    return fetch({
        url: '/users/'+id,
        method: 'get'
    });
}

export function getProfile(token){
    return fetch({
        url: '/users',
        method: 'get',
        params: { token }
    });
}

/**
 * params = {
        pageIndex:0,
        pageSize:20,
        ...
    }
 */
export function list(params){
    return fetch({
        url: '/users',
        method: 'get',
        params
    });
}

export function create(username='', password, parentID, name, role){
    const data = {
        username,
        password,
        parentID,
        name,
        role
    }
    return fetch({
        url: '/users/create',
        method: 'post',
        data
    });
}

export function update(id, name, role){
    const data = {
        name,
        role
    }
    return fetch({
        url: '/users/'+id+'/update',
        method: 'post',
        data
    });
}

export function changePassword(id, oldPassword, newPassword){
    const data = {
        oldPassword,
        newPassword
    }
    return fetch({
        url: '/users/'+id+'/changepassword',
        method: 'post',
        data
    });
}

export function addGroup(id, group_id){
    const data = {
        group_id,
    }
    return fetch({
        url: '/users/'+id+'/addgroup',
        method: 'post',
        data
    });
}

export function del(id){
    return fetch({
        url: '/users/'+id,
        method: 'post',
    });
}


